{% extends 'AdminBundle::layout.html.twig' %}

    {% block title %}{% if data.data.info.id == 0 %}发布文件{% else %}编辑文件{% endif %}{% endblock %}

    {% block css %}
        <style>
            .com-list-box{
                width: 725px;
            }
            .display{
                width: 100%;
            }

            /*2017-9-4*/
            .pop-add-slide{
                height: 205px;
            }
            .mes-select{
                width: 100%;
            }
            .pop-add-top .mes-form{
                margin-top: 5px;
                margin-left: 10px;
                margin-right: 10px;
            }
            .pop-add-top .mes-input{
                margin-left: 10px;
                margin-right: 10px;
            }
            .mes-query-btn{
                clear:both;
                display: block;
                margin: auto;
                float:initial;
                cursor:pointer;
            }
        </style>
    {% endblock %}



{% block top %}
    {#{{ dump() }}#}
    {{ parent() }}
{% endblock %}


    {% block left_body_top %}
        {{ parent() }}
    {% endblock %}


    {% block left_body %}
        {{ parent() }}
    {% endblock %}

    {% block right_body %}
    <div class="page-header">
        <div class="page-right-top">
            <div class="right-top">
                <span class="right-title">文件管理</span>
            </div>
            <div class="top-nav top-nav-active">{% if data.data.info.id == 0 %}发布文件{% else %}编辑文件{% endif %}</div>
        </div>
        <div class="main-content">
            <div class="mes-content">

                <div class="main-mes-content">
                    <form id="comMesBtn">
                        <div class="com-list">

                            <div class="com-list-box">
                                <div class="com-left fl">文件标题</div>
                                <input type="text" name="title" class="is-change file-title pop-com-input" value="{% if data.data.info.id != 0 %}{{ data.data.info.title }}{% endif %}" maxlength="50" placeholder="请输入文件标题">
                                <span class="pop-com-num">50</span>
                            </div>

                            <div class="com-list-box">
                                <div class="com-left fl">标题关键词</div>
                                <input type="text" name="keyword_input" class="pop-com-key file-key-value" value="{% if data.data.info.id != 0 %}{{ data.data.info.keyword }}{% endif %}" placeholder="请输入文件标题关键词（逗号分开）">
                                <span class="pop-com-num" style="width: 110px;">最多5个关键词</span>
                            </div>

                            <div class="com-list-box">
                                <div class="com-left fl">文件概要</div>
                                <textarea name="outline" class="is-change file-summary pop-com-textarea" placeholder="请输入文件概要" maxlength="50" cols="30" rows="4">{% if data.data.info.id != 0 %}{{ data.data.info.summary }}{% endif %}</textarea>
                                <span class="pop-com-num" style="bottom: 10px;top: inherit;">50</span>
                            </div>
                            <div class="display">
                                <div class="com-list-box fl">
                                    <div class="com-left fl">选择文件夹</div>
                                    <select class="com-select fl fileCategory" name="fileCategory">
                                        <option value="">请选择文件夹</option>

                                    </select>

                                    <button class="lang-btn fl" type="button">新建文件夹</button>

                                    <div class="clearfix"></div>
                                </div>

                                <div class="clearfix"></div>
                            </div>

                            <div class="display">
                                <div class="com-list-box fl">
                                    <div class="com-left fl">附件</div>
                                    <label class="upload-btn fl" for="upload_file" style="margin: 0;">点击上传</label>

                                    <div class="upload-finish">

                                        {% if data.data.info.id > 0 %}
                                        <div class="g-left-list" style="margin: 1px 10px">
                                            <div style="width: 100%;" class="a-line">
                                                <span class="p-name">{{ data.data.info.attachment_name|default('') }}</span>
                                            </div>
                                            <span class="reset-icon g-left-icon"></span>
                                        </div>
                                        {% endif %}

                                    </div>

                                    <input type="file" id="upload_file">
                                    <div class="clearfix"></div>
                                </div>
                                <div class="clearfix"></div>
                            </div>

                            <div class="display">
                                <div class="com-list-box fl">
                                    <div class="com-left fl">查看权限</div>
                                    <div class="select-jurisdiction fl">
                                        <span class="radio-icon {% if data.data.info.id != 0 %}{% if data.data.info.is_public == 1 %}radio-icon-check{% endif %}{% else %} radio-icon-check {% endif %} fl" data-value="1"></span>
                                        <span class="file-text fl">公开文件</span>
                                    </div>

                                    <div class="select-jurisdiction fl">
                                        <span class="radio-icon fl {% if data.data.info.id != 0 %}{% if data.data.info.is_public == 0 %}radio-icon-check{% endif %}{% endif %}" data-value="0"></span>
                                        <span class="file-text fl">私密文件</span>
                                    </div>
                                </div>
                                <div class="clearfix"></div>
                            </div>

                            <div class="display">
                                <div class="com-list-box fl">
                                    <div class="com-left fl">发送给</div>
                                    <div id="collect-mes" class="collect-mes-box">
                                        <div class="collect-mes"></div>
                                        <span class="send-out fl" style="margin: 16px 0 0 10px;"></span>

                                        <div class="clearfix"></div>
                                    </div>

                                    <div class="clearfix"></div>
                                </div>
                                <div class="clearfix"></div>
                            </div>

                            <div class="clearfix"></div>
                            <div class="add-mem-box" style="margin-top: 40px;">
                                <div class="com-left fl"></div>
                                <div class="acc-opera-btn fl mem-btn p-first-btn">发布</div>
                                <a href="{{ path('file_category') }}" class="acc-opera-btn fl mem-cancel apply-cancel">取消</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="pop-status opacity"></div>

    <!--新建文件夹信息-->
    <div class=" pop-classify pop-file-category" style="display: none;">
        <div class="pop-title">添加文件夹
            <span class="close close-icon"></span>
        </div>
        <form id="classSubmit">
            <div style="padding: 30px;">
                <div class="com-left fl" style="width: 86px;">文件夹名称</div>
                <input type="text" class="com-select com-is-name" placeholder="请输入文件夹名称" style="width: 350px;padding: 0 15px;">

                <div class="add-mem-box" style="margin:30px 0;overflow: hidden;">
                    <div class="com-left fl"></div>
                    <div class="acc-opera-btn fl mem-btn classify-btn">保存</div>
                    <div class="acc-opera-btn fl mem-cancel">取消</div>
                </div>
            </div>
        </form>
    </div>

    <!--接收文件人员-->
    <div class="pop-first pop-receive-member" style="display: none;">
        <div class="pop-title">选择接收培训通知人员
            <span class="close close-icon"></span>
        </div>
        <div style="padding: 20px;">
            <div class="pop-add-fl fl">

            </div>
            <div class="pop-add-fr">
                <form id="userListForm">
                    <div class="pop-add-top">
                        {#<div class="screen-term">#}
                            {#<select class="screen-de" name="" id="">#}
                                {#<option value="">筛选条件</option>#}
                                {#<option value="企业类别">企业类别</option>#}
                                {#<option value="企业载体">企业载体</option>#}
                                {#<option value="条件四">状态</option>#}
                                {#<option value=""></option>#}
                            {#</select>#}
                        {#</div>#}

                        <div class="mes-input fl" style="width: 138px;">
                            <input type="text" class="keyword-input" style="width: 120px;" name="user_name" placeholder="请输入联络人姓名">
                        </div>

                        <div class="mes-input fr" style="width: 138px;">
                            <input type="text" class="keyword-input" style="width: 120px;" name="company_name" placeholder="请输入企业名称">
                        </div>

                        <div class="fl mes-form" style="width: 138px;">
                            <select name="company_category_id" class="mes-select fl">
                                <option value="">请选择企业类别</option>
                                {% for item in data.category %}
                                    <option value="{{ item.id }}">{{ item.company_category_name }}</option>
                                {% endfor %}
                            </select>
                        </div>


                        <div class="fr mes-form" style="width: 138px">
                            <select name="company_attribute_id" class="mes-select fl">
                                <option value="">请选择企业属性</option>
                                {% for item in data.attribute %}
                                    <option value="{{ item.id }}">{{ item.company_attribute_name }}</option>
                                {% endfor %}
                            </select>
                        </div>

                        <div class="fl mes-form" style="width: 138px;margin-bottom:8px">
                            <select name="user_admin_id" class="mes-select fl">
                                <option value="">请选择科技载体</option>
                                {% for item in data.teac_admin %}
                                    <option value="{{ item.id }}">{{ item.nickname }}</option>
                                {% endfor %}
                            </select>
                        </div>


                        <div class="fr mes-form" style="width: 138px;margin-bottom:8px">
                            <select name="is_tech_user" class="mes-select fl">
                                <option value="">联络人身份属性</option>
                                <option value="1">科技联络人</option>
                                <option value="2">其他联络人</option>
                            </select>
                        </div>

                        <div class="mes-query-btn fl">查询</div>
                    </div>
                </form>

                <div class="pop-add-slide">
                    <div class="choose-top">
                        <input type="checkbox" id="allcbox" class="fl">
                        <span class="choose-all">全选</span>
                    </div>


                </div>
            </div>

            <button class="pop-g-btn">确定</button>

            <div class="clearfix"></div>
        </div>
    </div>


    {% endblock %}


{% block box %}
{% endblock %}


{% block javascript %}
<script type="text/javascript">
    $(function () {

        SidebarHighlighting('6');

        var attachment_id = "{{ data.data.info.attachment_id|default("") }}";

        // 监听input值的变化
        $('.is-change').keyup(function() {
            var this_val_len = $(this).val().length;

            $(this).next('.pop-com-num').text(50 - this_val_len);

        });

        // 权限选择
        $('.radio-icon').on('click', function () {
            $('.radio-icon').removeClass('radio-icon-check');
            $(this).addClass('radio-icon-check');
        });

        // 点击新建文件夹
        $('.lang-btn').on('click', function() {
            ComMesOpen();
            $('.pop-file-category').show();
        });

        // 添加文件分类
        $('.classify-btn').on('click', function () {
            var title = $(".com-is-name").val();

            if ($.trim(title) == '') {
                popStatus(2, '请输入名称', 1, '', true);
                return;
            }

            $.post("{{ path('api_admin_add_file_category') }}",{title:title},function(data){
                console.log(data);
                if(data.errorCode == 0){
                    var status = 1;
                } else {
                    var status = 2;
                }
                popStatus(status, data.message, 1, '', true);
            })

            $(".com-is-name").val('');

            ComMesClose();

            //重新获取文件分类
            $(".add-category").remove();
            getFileCate();
        });

        //点击取消
        $('.mem-cancel, .close-icon').on('click', function() {
            ComMesClose();
        });

        //  点击发送icon
        $('.send-out').on('click', function() {

            if($(".radio-icon-check").data("value") == 0){
                popStatus(2,"私密文件不能选择接收人",1.5,"",true);
                return;
            } else {
                ComMesOpen();
                $('.pop-receive-member').show();
            }

        });

        // 添加接受人  点击保存
        $('.phone-btn').on('click', function() {
            var is_name = $('.collect-name');
            var is_phone = $('.collect-phone');

            if ($.trim(is_name.val()) == '' || $.trim(is_phone.val()) == '') {
                popStatus(2, '请填写完整', 1, '', true);
                return;
            }

            popStatus(1, '添加成功', 1, '', true);

            ComMesClose();

            var html = '<div class="phone-box fl">'+ is_name.val() +''+ is_phone.val() +'</div>';

            $('#collect-mes').prepend(html);

            is_name.val(''); is_phone.val('');

        });

        // 弹框显示函数
        function ComMesClose() {
            $('.pop-status').hide();
            $('.pop-second').hide();
            $('.pop-first').hide();
            $(".pop-file-category").hide();
        }

        function ComMesOpen() {
            $('.pop-status').show();
        }


        // 获取文件分类列表
        function getFileCate(){

            var file_category_id = "{{ data.data.info.file_category_id|default('') }}";


            $.getJSON("{{ path('api_admin_get_file_category') }}",{},function(data){
                console.log(data);
                var item = data.data.info;
                for(var i in item){

                    var html = '';

                    if(file_category_id == item[i].id){
                        html += '<option class="add-category" selected value="'+ item[i].id +'">'+ item[i].name +'</option>';
                    } else {
                        html += '<option class="add-category" value="'+ item[i].id +'">'+ item[i].name +'</option>';
                    }

                    $(".fileCategory").append(html);
                }

            })
        }
        getFileCate();


        // ajax 上传附件
        function uploadFile() {
            var _file = this;
            var data = new FormData();
            data.append('attachment', _file.files[0]);

            URL_fileShareUpload = "{{ path('api_public_upload_attachment') }}";
            $.ajax({
                url:URL_fileShareUpload,
                type: 'POST',
                dataType: 'json',
                processData: false,
                contentType: false,
                data: data,
                success: function (data) {
                    if(data.errorCode == 0){

                        $('.upload-finish').html('');

                        attachment_id = data.data.attachment_id;
                        popStatus(1, '上传成功', 1,'', true);

                        var html = '<div class="g-left-list" style="margin: 1px 10px">' +
                                    '<div style="width: 100%;" class="a-line">' +
                                    '<span class="p-name">'+ data.data.name +'</span> ' +
                                    '<span class="p-phone"></span>' +
                                    '</div><span class="reset-icon g-left-icon">' +
                                    '</span>' +
                                    '</div>';

                        $('.upload-finish').html(html);

                    } else {
                        alert(data.message);
                        return false;
                    }
                }
            });
        }
        $("#upload_file").on("change", uploadFile);

        // 点击删除 选中的文件名
        $('body').on('click', '.reset-icon.g-left-icon', function () {
            $(this).parent('.g-left-list').remove();
            attachment_id = "";

        });

        // 删除选中的人员
        $("body").on("click","reset-user-list",function(){

            // 修改右侧列表的选中状态
            var re_id = $(this).parent('.g-left-list').data("id");
            $(".g-choose-list").each(function(){
                var right_this = $(this);
                var right_id = right_this.data("id");
                if(right_id == re_id){
                    right_this.find("input[name='sid[]']").prop("checked",false);
                }
            })
        })

        // 发布
        $(".p-first-btn").click(function(){

            var id = "{{ data.data.info.id }}";

            var title = $(".file-title").val();

            var keyword = $(".file-key-value").val();
            var summary = $(".file-summary").val();

            var is_public = $(".radio-icon-check").data("value");

            var file_category_id = $(".fileCategory").val();

            var key_arrs = keyword.split("，");

            //console.log(key_arrs);

            if(title == ""){
                popStatus(2, "请输入标题", 1,'', true);
                return;
            }
            if(keyword == ""){
                popStatus(2, "请输入关键字", 1,'', true);
                return;
            }

            if(key_arrs.length > 5){
                popStatus(2, "关键字不能超过5个", 1,'', true);
                return;
            }

            if(summary == ""){
                popStatus(2, "请输入简介", 1,'', true);
                return;
            }

            if(file_category_id == ""){
                popStatus(2, "请选择文件分类", 1,'', true);
                return;
            }

            if(attachment_id == ""){
                popStatus(2, "请上传附件", 1,'', true);
                return;
            }

            var prame = {id:id,title:title,keyword:keyword,summary:summary,attachment_id:attachment_id,is_public:is_public,file_category_id:file_category_id};

            // 接收人员数据
            if(is_public == "1"){
                var is_arr = [];
                $('.collect-mes .g-left-list').each(function() {

                    var $this = $(this);
                    var p_id = $this.data('id');
                    is_arr.push(p_id);
                });

                if(is_arr.length == 0){
                    // 全部人员
                    is_arr = all_users_id
                }

                var user_ids = JSON.stringify(is_arr);

                prame.user_ids = user_ids;
            }


            $.post("{{ path('api_admin_edit_file') }}",prame,function(data){
                console.log(data);

                if(data.errorCode == 0){
                    var status = 1;
                    var location = "{{ path('file_category') }}";
                } else {
                    var status = 2;
                    var location = "";
                }
                popStatus(status, data.message, 1,location, true);

            })
        });


        // 发送给
        var $subBox = $("input[name='sid[]']");

        //点击全选
        $("#allcbox").click(function() {
            $("input[name='sid[]']").prop("checked",this.checked);

            $('.pop-add-fl').empty();
            $("input[name='sid[]']").each(function() {
                var $this = $(this);
                if ($this.is(':checked')) {

                    getListData($this);
                }
            })
        });

        // 点击checkbox框
        $("body").on("click","input[name='sid[]']",function(){
            $("#allcbox").prop("checked",$subBox.length == $("input[name='sid[]']:checked").length ? true : false);

            var box_id = $(this).data('m');

            if ($(this).is(':checked')) {

                getListData($(this));

            } else {
                $('.g-left-list').each(function() {
                    var $this = $(this);

                    if ($this.data('id') == box_id) {
                        $this.remove();
                    }
                })
            }
        });

        // 点击左上角叉号

        $('body').on('click', '.g-left-icon', function() {
            var $this = $(this);

            var list_id = $this.parent('.g-left-list').data('id');

            $this.parent('.g-left-list').remove();

            $("input[name='sid[]']").each(function() {
                var $this = $(this);
                if (list_id == $this.data('m')) {
                    $this.prop("checked", false);
                }
            });

            $("#allcbox").prop("checked",$subBox.length == $("input[name='sid[]']:checked").length ? true : false);
        });

        //  点击确定
        $('.pop-g-btn').on('click', function() {

            $('.collect-mes').empty();
            ComMesClose();

            var html = $('.pop-add-fl').html();

            $('.collect-mes').prepend(html);

        });

        // 点击发布
        $('.p-first-btn').on('click', function() {
            var is_arr = [];
            // 接收人员数据
            $('.collect-mes .g-left-list').each(function() {

                var $this = $(this);
                var p_id = $this.data('id');
                is_arr.push(p_id);
            });

            var is_member = JSON.stringify(is_arr);

            var title = $('.pop-com-input').val();

            var content = $('.is-change.pop-com-textarea').val();
        });

        //  向弹窗左侧追加数据
        function getListData(isAajax) {
            var is_num = isAajax.data('m');
            var is_name = isAajax.data('n');
            var is_phone = isAajax.data('p');

            var html = '<div class="g-left-list" data-id="'+ is_num +'">\
                            <div style="width: 100%;" class="a-line">\
                                <span class="p-name">'+ is_name +'</span>\
                                <span class="p-phone">'+ is_phone +'</span>\
                            </div>\
                            <span class="reset-icon g-left-icon reset-user-list"></span>\
                        </div>';

            $('.pop-add-fl').prepend(html);
        }


        // 获取消息接收人员列表
        var all_users_id = [];
        function getMemberList(prame){

            $(".pop-add-slide").empty();
            console.log(prame);

            if(prame != undefined){
                prame_date = prame;
            } else {
                prame_date = {};
            }
            $.getJSON("{{ path('api_admin_get_user_list') }}",prame_date,function(data){

                console.log(data);
                var  item = data.data;
                for(var i in item){
                    all_users_id.push(item[i].id);

                    var html = '';
                    html += '<div class="g-choose-list" data-id="'+ item[i].id +'">';

                    html += '<input name="sid[]" autocomplete="off" type="checkbox" class="fl" data-m="'+ item[i].id +'" data-n="'+ item[i].name +'" data-p="'+ item[i].phone +'">';
                    html += '<div class="g-message">';
                    html += '<span class="g-name">'+ item[i].name +'</span>';
                    html += '<span class="g-name">'+ item[i].phone +'</span>';
                    html += '<span class="g-name">'+ item[i].company_full_name +'</span>';
                    html += '</div>';
                    html += '</div>';

                    $(".pop-add-slide").append(html);
                }

                //选中已选择人员
                judgeSelected();

            })


        }
        getMemberList();

        // 人员查询
        $(".mes-query-btn").click(function(){
            var prame = $("#userListForm").serializeObject();
            getMemberList(prame);

        })

        function judgeSelected(){
            // 判断该人员是否被选择
            $(".g-choose-list").each(function(){
                var search_list = $(this);
                var search_id = search_list.data("id");

                $(".g-left-list").each(function(){
                    var g_this = $(this);
                    var g_id = g_this.data("id");
                    if(g_id == search_id){
                        search_list.find("input[name='sid[]']").prop("checked",true);
                    }

                })


            })

        }


    })
</script>
{% endblock %}